<?php
declare (strict_types = 1);

namespace app\controller\admin;

use think\facade\Validate;
use think\response\Json;
use app\model\Apply;
use tp5er\Backup\facade\Backup;

class AdminBackup extends AdminBase
{

	protected $middleware = [
		'app\middleware\AdminLogin',
		'app\middleware\AdminAuth'
	];

	private string $menuName = '数据备份';

    /**
     * 显示资源列表
     * @throws
     * @return Json
     */
    public function index():Json
    {
        $this->result['data'] = Backup::tables();


        return returnAdmin($this->request,$this->result,$this->menuName.'-列表',$this->adminInfo);
    }

    /**
     * 备份
     * @throws
     * @return Json
     */
    public function backup():Json
    {
        //获取form表单
        $tables = input('tables/a',[]);

        try{
            if(!count($tables)){
                $tableArr = Backup::tables();
                foreach ($tableArr as $t){
                    $tables[] = $t['Name'];
                }
            }

            Backup::backup($tables);

        }catch (\Exception $e){
            $this->result['code'] = 500;
            $this->result['msg'] = $e->getMessage();
        }
        return returnAdmin($this->request,$this->result,$this->menuName.'-备份',$this->adminInfo);
    }

    /**
     * 优化
     * @throws
     * @return Json
     */
    public function optimize():Json
    {
        //获取form表单
        $tables = input('tables/a',[]);

        try{
            if(!count($tables)){
                $tableArr = Backup::tables();
                foreach ($tableArr as $t){
                    $tables[] = $t['Name'];
                }
            }

            Backup::optimize($tables);

        }catch (\Exception $e){
            $this->result['code'] = 500;
            $this->result['msg'] = $e->getMessage();
        }
        return returnAdmin($this->request,$this->result,$this->menuName.'-优化表',$this->adminInfo);
    }

    /**
     * 修复
     * @throws
     * @return Json
     */
    public function repair():Json
    {
        //获取form表单
        $tables = input('tables/a',[]);

        try{
            if(!count($tables)){
                $tableArr = Backup::tables();
                foreach ($tableArr as $t){
                    $tables[] = $t['Name'];
                }
            }

            Backup::repair($tables);

        }catch (\Exception $e){
            $this->result['code'] = 500;
            $this->result['msg'] = $e->getMessage();
        }
        return returnAdmin($this->request,$this->result,$this->menuName.'-修复表',$this->adminInfo);
    }


}
